/*********************************************************************************
 *      Copyright:  (C) 2023 Yangpeng
 *                  All rights reserved.
 *
 *       Filename:  get_temperature.c
 *    Description:  This file get temperature
 *                 
 *        Version:  1.0.0(2023年01月05日)
 *         Author:  Yangpeng <1023769078@qq.com>
 *      ChangeLog:  1, Release initial version on "2023年01月05日 16时39分48秒"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netdb.h>
#include <stdlib.h>
#include <fcntl.h>
#include <libgen.h>
#include <netinet/in.h>
#include <mosquitto.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <mosquitto.h>

#include "get_temperature.h"

#define BUF_SIZE    1024
#define ALIVE_Time 60
#define hostname "localhost"

int g_stop=0;

void printf_usage(char *program);
void sig_handler(int SIG_NUM);
int get_time(char *tim);

int main(int argc,char **argv)
{
    int     		daemon_run=0;
    int     		port;
    int     		opt;
    char       		*topic = NULL;
    char    		tim[32];
    float   		temper;
    char 		buf[512];
    char 		tem[32];
    char 		*user="Yangpeng";
    struct mosquitto    *mosq = NULL;
    int 		mid;

    char *program = basename(argv[0]);
    
    struct option long_options[] =
    {
        {"port",required_argument, NULL, 'p'},
        {"help",no_argument, NULL,'h'},
        {NULL, 0, NULL, 0}
    };
    while ((opt = getopt_long(argc, argv, "p:h", long_options, NULL)) != -1)
    {
         switch (opt)
        {
             case 'p':
                port = atoi(optarg);
                break;
             case 'h':
                printf_usage(argv[0]);
                break;
            default:
                break;
        }
    }
    if(!port)
    {
        printf_usage(program) ;
        return 0 ;
    }

    //安装信号
    signal(SIGUSR1, sig_handler);
    while(!g_stop)
    {
        if(get_temperature(&temper)<0)
        {
            printf("get_temperature() failed\n") ;
            return -1;
        }
        sprintf(tem,"  temperature:%5.0f",temper);
        get_time(tim);
        memset(buf,0,sizeof(buf));
        snprintf(buf,sizeof(buf),"%s%s%s",user,tim,tem);
        printf("%s\n",buf);


        mosquitto_lib_init();
        mosq = mosquitto_new("pub_test", true, NULL) ;
        if(mosq == NULL)
        {
            printf("New sub_test error!\n");
            mosquitto_lib_cleanup();
            return -1;
        }
        printf("Create mosquitto sucessfully!\n");
        
        if(mosquitto_connect(mosq,hostname,port,ALIVE_Time)!= MOSQ_ERR_SUCCESS)
        {
            printf("Mosq_Connect() failed: %s\n", strerror(errno) );
            mosquitto_destroy(mosq);
            mosquitto_lib_cleanup();
            return -2;
        }
        printf("Connect %s:%d Sucessfully!\n", hostname, port);
        int loop = mosquitto_loop_start(mosq);
        if(loop != MOSQ_ERR_SUCCESS)
        {
            printf("mosquitto loop error\n");
            mosquitto_destroy(mosq) ;
            mosquitto_lib_cleanup() ;
            return -3;
        }   
        if( mosquitto_publish(mosq,&mid,"topic",strlen(buf),buf,0,0) != MOSQ_ERR_SUCCESS ) 
        {
            printf("Mosq_Publish() error: %s\n", strerror(errno));
            mosquitto_destroy(mosq) ;
            mosquitto_lib_cleanup() ;
            return -4;
        }
        else
             printf("Publish information of temperature Ok!\n") ;
        sleep(5);
    }

}

void printf_usage(char *program)
{
    printf("使用方法：%s【选项】 \n", program);
    printf("\n传入参数\n");
    printf(" -p[port ] 指定连接的端口号\n");
    printf(" -h[help ] 打印帮助信息\n");
    printf("\n例如: %s -b -p 8900\n", program);
    return;
}


void sig_handler(int SIG_NUM)
{
    if(SIG_NUM == SIGUSR1)
    g_stop = 1 ;
}

int get_time(char *tim)
{
    time_t time_val,time_mk;
    struct tm *time_gm,*time_local;
    time(&time_val);
    time_gm=gmtime(&time_val);                      
    sprintf(tim,"%04d/%02d/%02d  %02d:%02d:%02d",time_gm->tm_year+1900,time_gm->tm_mon+1,time_gm->tm_mday,time_gm->tm_hour,time_gm->tm_min,time_gm->tm_sec);
    return 0;
}
